<?php

FLEA::loadClass('Controller_Public');

class Controller_MngrArtist extends Controller_Public
{	
	
	var $tableArtists;
	
	function Controller_MngrArtist()
    {		
		$this->tableArtists = FLEA::getSingleton('Model_Artists');
	}
	
	function actionList()
	{
		if (isset($_GET['result'])) {
            $result = (int)$_GET['result'];
        }

		$args = $_GET['args'];
		
		if ($result > 0) {
			if ( $args == 'add' )
				$message = "<div id=\"message\" class=\"updated fade\"><p><strong>乐队添加成功。</strong> </p></div>";
			else if ($args == 'edit')
				$message = "<div id=\"message\" class=\"updated fade\"><p><strong>乐队修改成功。</strong> </p></div>";
		} else {
			$message = '';
		}
		
		$smarty = &$this->_getView();
		$smarty->assign('menuName', $this->getMenuName());
		$smarty->assign('artistList', $this->tableArtists->findAll(null, 'artist_id DESC'));
		$smarty->assign('message', $message);		
		$smarty->display($this->getTemplates());
	}
	
	function actionAdd()
	{
		$tableCountry = &FLEA::getSingleton('Model_Country');		
		$rowset = $tableCountry->findAll(null, 'country_id ASC');

		FLEA::loadHelper('array');
		$countries = array_to_hashmap($rowset, 'country_name', 'country_id');

		$smarty = &$this->_getView();
		$smarty->assign('menuName', $this->getMenuName());
		$smarty->assign('countries', $countries);
		$smarty->display($this->getTemplates());
	}

	function actionAdded()
	{
		$data = array(
            'artist_name' => $_POST['artist_name'],
            'country_id' => $_POST['country_id'],
			'artist_intro' => $_POST['artist_intro'],
        );

		$ret = $this->tableArtists->save($data);	
		
		redirect(url('mngrartist', 'list',array('result' => $ret,'args'=>'add')));
		
	}

	function actionEdit()
	{
		if (isset($_GET['artist_id'])) {
            $artist_id = (int)$_GET['artist_id'];
        }

		$condition = 'artist_id = '.$artist_id;
		$artist = $this->tableArtists->find($condition);

		$tableCountry =& FLEA::getSingleton('Model_Country');		
		$countries = $tableCountry->findAll(null, 'country_id ASC');
		
		$smarty = &$this->_getView();
		$smarty->assign('menuName', $this->getMenuName());
		$smarty->assign('artist', $artist);
		$smarty->assign('countries', $countries);
		$smarty->display($this->getTemplates());
	}

	function actionUpdate()
	{
		/*if (isset($_GET['artist_id'])) {
            $artist = $this->tableArtists->find((int)$_GET['artist_id']);
        }*/

		$data = array(
			'artist_id' => $_POST['artist_id'],
            'artist_name' => $_POST['artist_name'],
            'country_id' => $_POST['country_id'],
			'artist_intro' => $_POST['artist_intro'],
        );

		$ret = $this->tableArtists->save($data);	
		redirect(url('mngrartist', 'list',array('result' => $ret,'args'=>'edit')));
		
	}

	/**
	 获取指定artist下的专辑
	*/
	function actionAlbum()
	{
		if (isset($_GET['artist_id'])) {
            $artist_id = (int)$_GET['artist_id'];
        }
		$tableAlbums = FLEA::getSingleton('Model_Albums');


		$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

		FLEA::loadClass('Controller_Pager');
		$condition = 'artist_id='.$artist_id;
		$pager = &new Controller_Pager($tableAlbums, $page-1,FLEA::getAppInf('pageSize'),$condition);

		$smarty = &$this->_getView();
		$smarty->assign('menuName', $this->getMenuName());
		$smarty->assign('albumList', $pager->findAll('album_id,album_name,artist_id,label_id,album_thumb,release_date', 'album_id DESC'));
		$smarty->assign('message', $message);	
        $smarty->assign('page_info',$pager->getNavBar(url('mngrartist','album',array('artist_id'=>$artist_id))) );
		$smarty->display('mngralbum_list.html');


		/*$smarty = &$this->_getView();
		$smarty->assign('menuName', $this->getMenuName());
		$smarty->assign('albumList', $tableAlbums->findAll('artist_id='.$artist_id, 'album_id DESC'));
		$smarty->display('mngralbum_list.html');*/
	}
	
		
} 